package com.gxa.hotel.admin.config;


import com.gxa.hotel.admin.filter.LoginFilter;
import com.gxa.hotel.admin.realm.AdminRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * ConditionalOnMissingBean spring提供的注解
 */
//@Configuration
public class ShiroConfig {
    @Bean
    @ConditionalOnMissingBean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
        defaultAAP.setProxyTargetClass(true);
        return defaultAAP;
    }
    //将自己的验证方式加入容器
//    @Bean
//    public AdminRealm adminRealm() {
//        AdminRealm adminRealm = new AdminRealm();
//        return adminRealm;
//    }
    @Autowired
    AdminRealm adminRealm;
    //权限管理，配置主要是Realm的管理认证

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new
                DefaultWebSecurityManager();
        securityManager.setRealm(adminRealm);
        return securityManager;
    }
    @Bean
    public ShiroFilterFactoryBean
    shiroFilterFactoryBean(SecurityManager securityManager) {
// 实例化shiro的过滤器工厂
        ShiroFilterFactoryBean shiroFilterFactoryBean =new ShiroFilterFactoryBean();
// 加载安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);
// 加载过滤器
        //shiro提供了一个自定义过滤器机制,改变登录不成功的处理方式:不用跳转到login.jsp
        Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();

        //希望:返回给前端json
        filters.put("authc",new LoginFilter());
// 认证规则的集合
        Map<String, String> map = new HashMap<>();
//对所有用户认证
        map.put("/**", "authc");
// 匿名访问的接口,anon表示匿名访问的过滤器
        //一般注册和登录可以匿名访问
        //注册:用户生成
        //登录:生成课认证和授权的信息
        map.put("/**/register","anon");
        map.put("/**/login","anon");
        map.put("/swagger-ui/**","anon");
        map.put("/doc.html","anon");
        map.put("/swagger-resources/**","anon");
        map.put("/webjars/**","anon");
        map.put("/v3/**","anon");
        map.put("/goods/**","anon");
        map.put("/upload/**","anon");
        //登录,当shiro发现用户不具备角色和权限的时候,会自动跳转
        //1.最初的方式:跳转到 login.jsp 页面
        //2. 通过setLoginUrl 指定备用的访问接口
        //3. 把需要的登录异常信息暴露给前端
        shiroFilterFactoryBean.setLoginUrl("/login");
//首页
// shiroFilterFactoryBean.setSuccessUrl("/index");
//错误页面，认证不通过跳转
//错误页面,认证不通关跳转
//        shiroFilterFactoryBean.setUnauthorizedUrl("/error");
// 加载认证规则
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor
    authorizationAttributeSourceAdvisor(SecurityManager
                                                securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}
